<template>
  <div class="person-info">
    <el-card class="info-card">
      <template #header>
        <div class="header">
          <span class="title">修改密码</span>
        </div>
      </template>

      <div class="info-content">
        <div class="password-section">
          <el-form
              :model="form"
              :rules="rules"
              ref="formRef"
              label-width="100px"
              class="password-form"
          >
            <el-form-item label="原密码" prop="oldPassword">
              <el-input v-model="form.oldPassword" type="password" show-password />
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
              <el-input v-model="form.newPassword" type="password" show-password />
            </el-form-item>
            <el-form-item label="确认新密码" prop="confirmPassword">
              <el-input v-model="form.confirmPassword" type="password" show-password />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm">确认修改</el-button>
              <el-button @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { post } from '@/net'

const formRef = ref(null)
const form = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const validateConfirmPassword = (rule, value, callback) => {
  if (value !== form.newPassword) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  oldPassword: [
    {required: true, message: '请输入原密码', trigger: 'blur'},
    {min: 6, message: '密码长度不能小于6位', trigger: 'blur'}
  ],
  newPassword: [
    {required: true, message: '请输入新密码', trigger: 'blur'},
    {min: 6, message: '密码长度不能小于6位', trigger: 'blur'}
  ],
  confirmPassword: [
    {required: true, message: '请确认新密码', trigger: 'blur'},
    {validator: validateConfirmPassword, trigger: 'blur'}
  ]
}

const submitForm = () => {
  // 从localStorage获取用户信息
  const userInfo = JSON.parse(localStorage.getItem('user'))
  formRef.value.validate((valid) => {
    if (valid) {
      post('/api/user/updatePassword', {
        username: userInfo.username,
        oldPassword: form.oldPassword,
        newPassword: form.newPassword
      }, (res) => {
        ElMessage.success('密码修改成功，请重新登录')
        localStorage.removeItem('user')
        localStorage.removeItem('token')
        window.location.href = '/'
      }, (err) => {
        ElMessage.error(err.message || '密码修改失败')
      })
    }
  })
}

const resetForm = () => {
  formRef.value.resetFields()
}
</script>

<style scoped>
.person-info {
  width: 1400px;
  margin: 0 auto;
  padding: 20px;
}

.info-card {
  background: #fff;
  border-radius: 8px;
  min-height: 600px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.info-content {
  padding: 20px;
}

.password-section {
  max-width: 800px;
  margin: 0 auto;
}

.password-form {
  max-width: 500px;
  margin: 0 auto;
}

.el-form-item:last-child {
  margin-bottom: 0;
  text-align: center;
}

.el-button {
  margin: 0 10px;
}
</style>